﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

namespace iuPlace.Web
{
    public class CacheResult : ActionResult
    {
        public CacheResult()
        {
            this.CacheAssets = new List<string>();
            this.NetworkAssets = new List<string>();
            this.FallbackAssets = new Dictionary<string, string>();
        }

        public string Version { get; set; }
        public List<string> CacheAssets { get; set; }
        public List<string> NetworkAssets { get; set; }
        public Dictionary<string,string> FallbackAssets { get; set; }


        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.HttpContext.Response;
            response.Cache.SetMaxAge(TimeSpan.Zero);
            response.ContentType = "text/cache-manifest";
            response.ContentEncoding = System.Text.Encoding.UTF8;
            response.Write(this.GenerateContent());
        }

        private string GenerateContent()
        {
            StringBuilder content = new StringBuilder();
            content.Append(this.GenerateHeader());
            content.Append(this.GenerateVersion());
            content.Append(this.GenerateCache());
            content.Append(this.GenerateNetwork());
            content.Append(this.GenerateFallback());
            return content.ToString();
        }

        private string GenerateHeader()
        {
            return "CACHE MANIFEST" + Environment.NewLine;
        }

        private string GenerateVersion()
        {
            return string.Format("{0}#{1}{0}", Environment.NewLine, this.Version);
        }

        private string GenerateCache()
        {
            var result = new StringBuilder();
            result.AppendLine();
            result.AppendLine("CACHE:");
            this.CacheAssets.ToList().ForEach(a => result.AppendLine(a));
            return result.ToString();
        }

        private string GenerateNetwork()
        {
            var result = new StringBuilder();
            result.AppendLine();
            result.AppendLine("NETWORK:");            
            if (this.NetworkAssets.Any())
            {
                this.NetworkAssets.ForEach(a => result.AppendLine(a));
            }
            else
            {
                result.AppendLine("*");
            }
            return result.ToString();
        }

        private string GenerateFallback()
        {
            if (!this.FallbackAssets.Any())
            {
                return string.Empty;
            }
            var result = new StringBuilder();
            result.AppendLine();
            result.AppendLine("FALLBACK:");
            foreach (var pair in this.FallbackAssets)
            {
                result.AppendLine(pair.Key + " " + pair.Value);
            }
            return result.ToString();
        }
    }
}